package com.community.demo.beans.administracion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.beanutils.BeanUtils;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.community.demo.beans.GenericBean;
import com.community.demo.datamodel.LazySorter;
import com.community.demo.model.EstadoZona;
import com.community.demo.model.TipoZona;
import com.community.demo.model.Zona;


/**
 * @author andres.alzate
 *
 */
@ManagedBean
@ViewScoped

public class ZonaBean extends GenericBean{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7437507439782500281L;
	
	/**
	 * Componente de la vista encargado de mostrar la lista de zonas. 
	 */
	private LazyDataModel<Zona> lazyDataModel;
	
	
	private Zona zonaSelected;
	
	
	public ZonaBean(){
		poblarListaZonas();
		createLazyDataModel();
	}
	/**
	 * Lista de zonas visible
	 */
	private List<Zona> listaZonas;
	
	public Zona zona;
	
	public Zona getZona(){
		if(this.zona == null){
			this.zona = new Zona();
			this.zona.setTipoZona(new TipoZona());
			this.zona.setEstadoZona(new EstadoZona()); 
		}
		
		return zona;
	}
	
	public List<TipoZona> getTiposZona(){
		return this.getZona().getTipoZona().getAll();
	}
	
	
	public List<EstadoZona> getEstadosZona(){
		return this.getZona().getEstadoZona().getAll();
	}
	
	
	public List<Zona> getZonas(){
		return this.getZona().getAll();
	}
	
	public String guardar(){
		
		System.out.println("GUARDADO");
		
		return "";
	}
	
	
	/**
	 * Pobla la lista de zonas de la vista.
	 * @return
	 */
	private List<Zona> poblarListaZonas(){
		
		Zona zona = new Zona();
		TipoZona tipoZona = new TipoZona();
		EstadoZona estadoZona = new EstadoZona();
		
		this.listaZonas = zona.getAll();
		List<TipoZona> listaTiposZona = tipoZona.getAll();
		List<EstadoZona> listaEstadoZonas = estadoZona.getAll();
		
		int random = new Double((Math.random() * listaTiposZona.size())).intValue();
		int random1 = new Double((Math.random() * listaEstadoZonas.size())).intValue();
		
		for (Iterator iterator = listaZonas.iterator(); iterator.hasNext();) {
			Zona zonaLista = (Zona) iterator.next();
			
			zonaLista.setTipoZona(listaTiposZona.get(random));
			zonaLista.setEstadoZona(listaEstadoZonas.get(random1));
		}
		return listaZonas;
	}
	
	
	
	  /**
     * Crea el lazy datamodel
     */
    private void createLazyDataModel(){
    	lazyDataModel = new LazyDataModel<Zona>() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			
		    @Override
		    public void setRowIndex(int rowIndex) {
		    	/*
		         * The following is in ancestor (LazyDataModel):
		         * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
		         */
		        if (rowIndex == -1 || getPageSize() == 0) {
		            super.setRowIndex(-1);
		        } else {
		            super.setRowIndex(rowIndex % getPageSize());
		        }
		    }
		    
			@Override
			public List<Zona> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
				List<Zona> data = new ArrayList<Zona>();  
				  
		        //filter  
		        for(Zona zona : listaZonas) {  
		            boolean match = true;  
		  
		            for(Iterator<String> key = filters.keySet().iterator(); key.hasNext();) {  
		                try {  
		                    String filterProperty = key.next();
		                    String filterValue = filters.get(filterProperty);  
		                    String fieldValue = BeanUtils.getProperty(zona, filterProperty);
		  
		                    if(filterValue == null || fieldValue.startsWith(filterValue)) {  
		                        match = true;  
		                    } else {  
		                        match = false;  
		                        break;  
		                    }  
		                } catch(Exception e) {  
		                    match = false;  
		                }   
		            }
		  
		            if(match) {  
		                data.add(zona);  
		            }  
		        }  
		  
		        //sort  
		        if(sortField != null) {  
		            Collections.sort(data, new LazySorter(sortField, sortOrder));  
		        } 
		  
		        //rowCount  
		        int dataSize = data.size();  
		        this.setRowCount(dataSize);  
		  
		        //paginate  
		        if(dataSize > pageSize) {  
		            try {  
		                return data.subList(first, first + pageSize);  
		            }  
		            catch(IndexOutOfBoundsException e) {  
		                return data.subList(first, first + (dataSize % pageSize));  
		            }  
		        }  
		        else {  
		            return data;  
		        }  
			}
		};
    }

	public LazyDataModel<Zona> getLazyDataModel() {
		return lazyDataModel;
	}

	public void setLazyDataModel(LazyDataModel<Zona> lazyDataModel) {
		this.lazyDataModel = lazyDataModel;
	}

	public Zona getZonaSelected() {
		return zonaSelected;
	}

	public void setZonaSelected(Zona zonaSelected) {
		this.zonaSelected = zonaSelected;
	}
    
    
    
    
	
}
